﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using iShoppingBUS;
using iShoppingDTO;

namespace iShopping.Guest.UserControl
{
    public partial class DSSanPham : System.Web.UI.UserControl
    {
        private List<SanPhamDTO> tb = new List<SanPhamDTO>();
        private double dtime = 86400;
        double totalSeconds = 0;
        int seconds = 0;
        int minutes = 0;
        private int hours = 0;
        string time = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                phanTrang();
                ViewState["Time"] = dtime;

            }
            else
            {
                dtime = (double)ViewState["Time"];
            }

        }
        public void phanTrang()
        {
            //string connString = "Data Source=DO-NGUYEN-PC\\;Initial Catalog=DauGia;Integrated Security=True";
            string connString = ConfigurationManager.ConnectionStrings["DauGia"].ConnectionString;
            try
            {
                SqlConnection conn = new SqlConnection(connString);
                conn.Open();
                //String maloaiSP = "LSP005";
                string maloaiSP = Request.QueryString["Value"].ToString();
                string sql = "SELECT * FROM SanPham WHERE MaLoaiSanPham = '" + maloaiSP + "'";
                SqlDataAdapter dtAdapter = new SqlDataAdapter(sql, conn);
                DataTable resTable = new DataTable();
                if (!IsPostBack)
                {
                    dtAdapter.Fill(resTable);
                    lblRecordCount.Text = Convert.ToString(resTable.Rows.Count);
                    resTable = null;
                    resTable = new DataTable();
                }
                dtAdapter.Fill(Convert.ToInt32(lblCurrentIndex.Text), Convert.ToInt32(lblPageSize.Text), resTable);
                DataList1.DataSource = resTable;
                DataList1.DataBind();
                conn.Close();
                conn.Dispose();
            }
            catch (Exception ex)
            {
            }

            hienThiDataList();
        }
        public void hienThiDataList()
        {
            lblCount.Text = "Số mẩu tin: <b>" + lblRecordCount.Text;
            lblCount.Text += "</b> | Trang: <b> ";
            lblCount.Text += Convert.ToString(Convert.ToInt32(lblCurrentIndex.Text) / Convert.ToInt32(lblPageSize.Text) + 1);
            lblCount.Text += " </b> trên tổng số <b>";
            if ((Convert.ToInt32(lblRecordCount.Text) % Convert.ToInt32(lblPageSize.Text)) > 0)
            {
                lblCount.Text += Convert.ToString(Convert.ToInt32(lblRecordCount.Text) / Convert.ToInt32(lblPageSize.Text) + 1);
            }
            else
            {
                lblCount.Text += Convert.ToString(Convert.ToInt32(lblRecordCount.Text) / Convert.ToInt32(lblPageSize.Text));
            }
            lblCount.Text += " </b> trang";
        }
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            lblCurrentIndex.Text = Convert.ToString(Convert.ToInt32(lblCurrentIndex.Text) - Convert.ToInt32(lblPageSize.Text));
            if (Convert.ToInt32(lblCurrentIndex.Text) < 0)
            {
                lblCurrentIndex.Text = "0";
            }
            phanTrang();
        }
        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            if ((Convert.ToInt32(lblCurrentIndex.Text) + Convert.ToInt32(lblPageSize.Text)) < Convert.ToInt32(lblRecordCount.Text))
            {
                lblCurrentIndex.Text = Convert.ToString(Convert.ToInt32(lblCurrentIndex.Text) + Convert.ToInt32(lblPageSize.Text));
            }
            phanTrang();
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            ViewState["Time"] = Convert.ToDouble(ViewState["Time"]) - 1;
            if (Convert.ToDouble(ViewState["Time"]) <= 0)
            {

            }
            else
            {
                totalSeconds = Convert.ToDouble(ViewState["Time"]);
                int temp = (int)totalSeconds / 60;
                seconds = (int)totalSeconds % 60;
                hours = temp / 60;
                minutes = temp % 60;

                time = hours + ":" + minutes + ":" + seconds;

                for (int i = 0; i < DataList1.Items.Count; i++)
                {
                    Label label = (Label)DataList1.Items[i].FindControl("Label1");
                    label.Text = time;

                }
            }
        }

        protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Guest/LoginPage.aspx");
        }

    }
}